package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.Protocol;
import com.caocaog.market.mapper.ProtocolMapper;
import com.caocaog.market.service.ProtocolService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 协议管理
 *
 * @author chechen
 * @date 2021-12-28
 */
@Service
@Transactional
public class ProtocolServiceImpl extends ServiceImpl<ProtocolMapper, Protocol> implements ProtocolService {

    @Override
    public IPage<Protocol> queryProtocolPage(Page<Protocol> page, Protocol protocol) {
        return super.baseMapper.queryProtocolPage(page, protocol);
    }

    @Override
    @Cacheable(value = CacheConstant.PROTOCOL_INFO, key = "#id", unless = "#result == null")
    public Protocol getProtocolById(Integer id) {
        return super.baseMapper.selectById(id);
    }

    @Override
    public boolean containsPrimaryKey(Integer id) {
        return super.baseMapper.selectCount(new LambdaQueryWrapper<Protocol>().eq(Protocol::getId, id)) > 0;
    }

    @Override
    public String addProtocol(Protocol protocol) {
        protocol.clearFields();
        protocol.setCreateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername());

        if (this.containsPrimaryKey(protocol.getType()))
            throw new JeecgBootException("库中已有此协议类型，请再更换一个");
        protocol.setId(protocol.getType());

        super.baseMapper.insert(protocol);
        if (protocol.getId() == null)
            throw new JeecgBootException("新增协议信息失败");
        return protocol.getId().toString();
    }

    @Override
    @CacheEvict(value = CacheConstant.PROTOCOL_INFO, key = "#protocol.id")
    public int updateProtocol(Protocol protocol) {
        protocol.clearFields();
        protocol.setUpdateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername());

        int updateRecordCount = super.baseMapper.updateById(protocol);
        if (updateRecordCount == 0)
            throw new JeecgBootException("更新协议信息失败, id-> " + protocol.getId());
        return updateRecordCount;
    }

    @Override
    @CacheEvict(value = CacheConstant.PROTOCOL_INFO, key = "#id")
    public int deleteProtocol(Long id) {
        int deleteRecordCount = super.baseMapper.deleteById(id);

        if (deleteRecordCount == 0)
            throw new JeecgBootException("删除协议信息失败, id-> " + id);
        return deleteRecordCount;
    }

}
